<?php


namespace app\mobile\service\base;


use app\admin\model\base\Practise;
use app\admin\model\base\StudentRanking;
use app\BaseService;
use think\response\Json;

class StudentRankingService extends BaseService
{
    /**
     * 获取排行版
     * @param array $params
     * @return Json
     */
    public function index(array $params): Json
    {
        $where = [];
        $where[] = ['is_del','=',0];
        $where2 = [];
        if(isset($params['name']) && $params['name']){
            $where[] = ['name','like',"%{$params['name']}%"];
        }
        if(isset($params['type']) && $params['type']){
            $where2[] = ['type','=',$params['type']];
        }

        $cate = Practise::where($where)->field('name,id')->select()->toArray();

        if(!empty($cate)){
            foreach ($cate as $k=>$item){
                $cate[$k]['ranking'] = StudentRanking::where($where2)
                    ->where('practise_id','=',$item['id'])
                    ->with(['student'])
                    ->order('score','desc')
                    ->field('score,student_id')
                    ->limit(10)->select()->toArray();
            }
        }
        return jsonReturn(1,$cate);
    }
}